package exatrez.config;

import able.guice.AbleContextListener;
import able.guice.AbleModule;
import com.google.inject.Injector;
import exatrez.quartz.AdminSchedulerManager;
import exatrez.quartz.PtaskSchedulerManager;
import exatrez.quartz.SchedulerManager;
import exatrez.quartz.Visit3SchedulerManager;
import exatrez.quartz.VisitSchedulerManager;
import exatrez.service.session.SessionManager;
import exatrez.stripes.config.ableActionBeanContext;
import javax.servlet.ServletContextEvent;
import net.sourceforge.stripes.action.ActionBeanContext;
import org.quartz.SchedulerException;



public class ableContextListener extends AbleContextListener {    

    SchedulerManager mgr;
    PtaskSchedulerManager ptmgr;
//    VisitSchedulerManager vmgr;
    Visit3SchedulerManager v3mgr;
    AdminSchedulerManager amgr;

     @Override
    public void contextInitialized(ServletContextEvent arg0) {
        super.contextInitialized(arg0);
        
        SessionManager sm = SessionManager.getInstance();
        sm.clear();
    }
    
    protected void afterGuiceInit(Injector injector) {
        try {
            mgr = injector.getInstance(SchedulerManager.class);
            mgr.start();
            System.out.println("[SchedulerManager] Start ... ");
            ptmgr = injector.getInstance(PtaskSchedulerManager.class);
            ptmgr.start();
            System.out.println("[PtaskSchedulerManager] Start ... ");
//            vmgr = injector.getInstance(VisitSchedulerManager.class);
//            vmgr.start();
            v3mgr = injector.getInstance(Visit3SchedulerManager.class);
            v3mgr.start();
            System.out.println("[Visit3SchedulerManager] Start ... ");
            amgr = injector.getInstance(AdminSchedulerManager.class);
            amgr.start();
            System.out.println("[AdminSchedulerManager] Start ... ");
        } catch (SchedulerException ex) {
            throw new RuntimeException("Scheduler Failed to start", ex);
        }

    }

    protected AbleModule getAbleModule() {
        return new ableModule();
    }

    protected Class<? extends ActionBeanContext> getActionBeanContextType() {
        return ableActionBeanContext.class;
    }  
    
     public void contextDestroyed(ServletContextEvent sce) {
        try {
            if (mgr != null) {
                System.out.println("[SchedulerManager] Shutdown ... ");
                mgr.shutdown();
            }
            if(ptmgr != null){
                System.out.println("[PtaskSchedulerManager] Shutdown ... ");
                ptmgr.shutdown();
            }
//            if(vmgr != null){
//                System.out.println("[VisitSchedulerManager] Shutdown ... ");
//                vmgr.shutdown();
//            }
            if(v3mgr != null){
                System.out.println("[Visit3SchedulerManager] Shutdown ....");
                v3mgr.shutdown();
            }
            if(amgr != null){
                System.out.println("[AdminSchedulerManager] Shutdown ... ");
                amgr.shutdown();
            }
        } catch (SchedulerException ex) {
            ex.printStackTrace();
            throw new RuntimeException("Scheduler Failed to stop");
        }
    }

    
}
